IF OBJECT_ID('ssf.job_get_run_status') IS NOT NULL
	DROP FUNCTION ssf.job_get_run_status
GO

CREATE FUNCTION ssf.job_get_run_status (
	@job_id UNIQUEIDENTIFIER
	, @run_datetime DATETIME
)
RETURNS INT
AS
BEGIN
/*
<object_header>

<object_summary>
	<purpose><![CDATA[
Return the status for the given job_id and run date/time.
	]]></purpose>
</object_summary>

</object_header>
*/

	RETURN (
		SELECT run_status
		FROM msdb.dbo.sysjobhistory WITH (NOLOCK)
		WHERE job_id = @job_id
		AND step_id = 0
		AND ssf.job_run_datetime(run_date, run_time) = @run_datetime
	)
END
GO
